package com.mxx.bu_yang.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.mxx.bu_yang.R;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者：刘江飞
 * 时间：2020/10/15  9:03
 * 邮箱：642371742Z@qq.com
 * 描述：日期筛选
 **/
public class DatePickDialog extends CancelSureDialog implements DatePicker.OnDateChangedListener, View.OnClickListener {
    DatePicker datePicker;
private Map<String,Object>paramsMap=new HashMap<>();
    /**
     * Sets layout id.
     *
     * @return the layout id
     */
    @Override
    protected int setLayoutId() {
        return R.layout.dialog_date_picker;
    }

    @Override
    protected void rightBtnSureClick() {

    }

    @Override
    protected void initView() {
        super.initView();
        if (mRootView == null) {
            return;
        }
        datePicker = mRootView.findViewById(R.id.date_picker);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            //bug1:日历模式，在5.0以下设置的可选时间区间如果与当前日期在同一栏会crash，所以只能用滚轮模式
            datePicker.setCalendarViewShown(false);
            datePicker.setSpinnersShown(true);
            //滚轮模式必须使用确定菜单
        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            //如果只要日历部分，隐藏header
            ViewGroup mContainer = (ViewGroup) datePicker.getChildAt(0);
            View header = mContainer.getChildAt(0);
            header.setVisibility(View.GONE);
        } else {
            //bug4:LOLLIPOP和Marshmallow上，使用spinner模式，然后隐藏滚轮，显示日历(spinner模式下的日历没有头部)，日历最底部一排日期被截去部分。所以只能使用calender模式，然后手动隐藏header（系统没有提供隐藏header的api）。
            //如果只要日历部分，隐藏header
            ViewGroup mContainer = null;
            try {
                mContainer = (ViewGroup) datePicker.getChildAt(0);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (mContainer == null) {
                return;
            }
            View header = mContainer.getChildAt(0);
            header.setVisibility(View.GONE);
            //Marshmallow上底部留白太多，减小间距
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) datePicker.getLayoutParams();
            layoutParams.bottomMargin = 10;
            datePicker.setLayoutParams(layoutParams);
        }

        initDatePicker();
    }

    private void initDatePicker() {
        if (datePicker == null) {
            return;
        }
        Calendar currentDate = Calendar.getInstance();
        currentDate.setTimeInMillis(System.currentTimeMillis());
        int year=currentDate.get(Calendar.YEAR);
        int month=currentDate.get(Calendar.MONTH);
        int day=currentDate.get(Calendar.DAY_OF_MONTH);

        datePicker.init(year, month,day , this);
    }


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int style;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            style = R.style.ZZBDatePickerDialogLStyle;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            style = R.style.ZZBDatePickerDialogLStyle;
        } else {
            style = getTheme();
        }
        return new Dialog(getActivity(), style);
    }


    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_sure:
                returnSelectedDateUnderLOLLIPOP();
                break;
            default:
                break;
        }
    }

    private void returnSelectedDateUnderLOLLIPOP() {
        //bug3:5.0上超过可选区间的日期依然能选中,所以要手动校验.5.1上已解决，但是为了与5.0保持一致，也采用确定菜单返回日期
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            Calendar selectedDate = Calendar.getInstance();
            selectedDate.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 0, 0, 0);
            selectedDate.set(Calendar.MILLISECOND, 0);
        }
        if (onSelectedDateListener != null) {
            int year=datePicker.getYear();
            int month=datePicker.getMonth() ;
            int day=datePicker.getDayOfMonth();
            onSelectedDateListener.onSelectedDate(year, month+1,day );
        }
        dismiss();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        onSelectedDateListener = null;
    }

    public interface OnSelectedDateListener {
        void onSelectedDate(int year, int monthOfYear, int dayOfMonth);
    }

    OnSelectedDateListener onSelectedDateListener;

    public void setOnSelectedDateListener(OnSelectedDateListener onSelectedDateListener) {
        this.onSelectedDateListener = onSelectedDateListener;
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { //LOLLIPOP上，这个回调无效，排除将来可能的干扰
            return;
        }
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { //5.0以下，必须采用滚轮模式，所以需借助确定菜单回传选定值
            return;
        }
        Log.i("TAG","do nothing");
    }

}
